package com.wangwei.modules.utils;

import java.util.Date;
import java.util.HashMap;

public class HttpResponseUtil {
    //http://tool.oschina.net/commons
    public enum ContentType {
        html("text/html"), gif("image/gif"), jpg("image/jpeg"), jpeg("image/jpeg"), css("text/css")

        , pdf("application/pdf"), png("application/x-png"), xml("text/xml"), apk("application/vnd.android.package-archive"), txt("text/plain"), doc("application/msword"), xls("application/vnd.ms-excel"), js("application/x-javascript"), DEFAULT("application/octet-stream"), json("application/json");



        String value;

        ContentType(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        public static ContentType getByName(String name) {
            int index = name.lastIndexOf(".");
            if (index != -1) {
                name = name.substring(index + 1);
            }
            switch (name.toLowerCase()) {
            case "css":
                return css;
            case "js":
                return js;
            case "json":
                return json;
            case "jpg":
                return jpg;
            case "png":
                return png;
            case "gif":
                return gif;
            default:
                return html;
            }
        }

    }

    public static byte[] getCodeMessageReturn(Integer code, String message) {

        byte[] data2 = message.getBytes();

        String ln = "\r\n";
        StringBuffer sb = new StringBuffer().append("HTTP/1.1 " + code + "" + ln).append("Content-Type: " + ContentType.html.value + "; charset=UTF-8" + ln).append("Content-Length: " + data2.length + ln).append("Date: " + DateUtil.getHttpDateString(new Date()) + ln)
                .append("Expires: " + DateUtil.getHttpDateString(new Date()) + ln).append("Cache-Control: private, max-age=0" + ln).append("X-Content-Type-Options: nosniff" + ln).append("X-Frame-Options: SAMEORIGIN" + ln).append(ln);

        byte[] data1 = sb.toString().getBytes();

        byte[] data3 = new byte[data1.length + data2.length];
        System.arraycopy(data1, 0, data3, 0, data1.length);
        System.arraycopy(data2, 0, data3, data1.length, data2.length);
        return data3;

    }

    public static byte[] addHttp1OKHeader(byte[] data2, ContentType contentType) {
        String ln = "\r\n";
        StringBuffer sb = new StringBuffer().append("HTTP/1.1 200OK" + ln).append("Content-Type: " + contentType.value + "; charset=UTF-8" + ln).append("Content-Length: " + data2.length + ln).append("Date: " + DateUtil.getHttpDateString(new Date()) + ln)
                .append("Expires: " + DateUtil.getHttpDateString(new Date()) + ln).append("Cache-Control: private, max-age=0" + ln).append("X-Content-Type-Options: nosniff" + ln).append("X-Frame-Options: SAMEORIGIN" + ln).append(ln)

        ;
        byte[] data1 = sb.toString().getBytes();

        byte[] data3 = new byte[data1.length + data2.length];
        System.arraycopy(data1, 0, data3, 0, data1.length);
        System.arraycopy(data2, 0, data3, data1.length, data2.length);
        return data3;
    }
}
